package zhang.test.sqlitedemo.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;
import android.util.Log;

import static zhang.test.sqlitedemo.data.FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE;

/**
 * Created by Administrator on 2017/1/10.
 */

public class LocalDataSource implements DataSource {
    private static final String TAG = "LocalDataSource";
    private DatabaseHelper mHelper;
    private static LocalDataSource INSTANCE;

    public LocalDataSource(Context context) {
        mHelper = new DatabaseHelper(context);
    }

    public static LocalDataSource getInstance(@NonNull Context context) {
        if (INSTANCE == null) {
            INSTANCE = new LocalDataSource(context);
        }
        return INSTANCE;
    }

    @Override
    public void add() {
        Log.d(TAG, "add: ");
        SQLiteDatabase db = mHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, "MyTitle");
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE, "二级菜单");
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_STUDENT, "学生");
        db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
        db.close();
    }

    @Override
    public void update() {
        Log.d(TAG, "update: ");
        SQLiteDatabase db = mHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE, "菜单");

        String selection = COLUMN_NAME_TITLE + " LIKE ?";
        String[] selectionArgs = { "MyTitle" };

        int count = db.update(
                FeedReaderContract.FeedEntry.TABLE_NAME,
                values,
                selection,
                selectionArgs);

        db.close();
        Log.d(TAG, "您修改了第 ： " + count);
    }

    @Override
    public void delete() {
        Log.d(TAG, "delete: ");
        SQLiteDatabase db = mHelper.getWritableDatabase();
        String selection = COLUMN_NAME_TITLE + " LIKE ?";

        String[] selectionArgs = { "MyTitle" };
        // Issue SQL statement.
        db.delete(FeedReaderContract.FeedEntry.TABLE_NAME, selection, selectionArgs);
        db.close();
    }

    @Override
    public void find() {
        SQLiteDatabase db = mHelper.getReadableDatabase();

        String[] projection = {
                FeedReaderContract.FeedEntry._ID,
                FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
                FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE
        };

        String selection = COLUMN_NAME_TITLE + " = ?";
        String[] selectionArgs = { "MyTitle" };

        String sortOrder =
                FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";

        Cursor c = db.query(
                FeedReaderContract.FeedEntry.TABLE_NAME,                     // The table to query
                projection,                               // The columns to return
                selection,                                // The columns for the WHERE clause
                selectionArgs,                            // The values for the WHERE clause
                null,                                     // don't group the rows
                null,                                     // don't filter by row groups
                null                                 // The sort order
        );

        Log.d(TAG, "find: " + c.getCount());
        if (c != null && c.getCount() > 0) {
            c.moveToFirst();
            String itemId = c.getString(c.getColumnIndexOrThrow(FeedReaderContract.FeedEntry._ID));
            String title = c.getString(c.getColumnIndexOrThrow(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE));
            String description =
                    c.getString(c.getColumnIndexOrThrow(FeedReaderContract.FeedEntry.COLUMN_NAME_SUBTITLE));
//            String student = c.getString(c.getColumnIndexOrThrow(COLUMN_NAME_STUDENT));

            Log.d(TAG, "id == " + itemId);
            Log.d(TAG, "title == " + title);
            Log.d(TAG, "description == " + description);
//            Log.d(TAG, "studnet == " + student);
        }
        if (c != null) {
            c.close();
        }

        db.close();
    }
}
